package org.jim.server.command.handler;

import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.StringUtils;
import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.ImStatus;
import org.jim.core.config.ImConfig;
import org.jim.core.exception.ImException;
import org.jim.core.message.MessageHelper;
import org.jim.core.packets.*;
import org.jim.core.utils.JsonKit;
import org.jim.server.JimServerAPI;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.processor.friend.FriendCmdProcessor;
import org.jim.server.protocol.ProtocolManager;
import org.jim.server.util.ImServerKit;

import java.util.List;
import java.util.Objects;

/**
 * 确认申请
 */
public class ConfirmApplyReqHandler extends AbstractCmdHandler {

    @Override
    public Command command() {
        return Command.COMMAND_CONFIRM_APPLY_REQ;
    }

    @Override
    public ImPacket handler(ImPacket packet, ImChannelContext imChannelContext) throws ImException {
        RespBody resPacket = new RespBody(Command.COMMAND_CONFIRM_APPLY_REQ, ImStatus.C10025);
        ConfirmApplyReqBody confirmApplyReqBody = JsonKit.toBean(packet.getBody(), ConfirmApplyReqBody.class);
        if (Objects.isNull(confirmApplyReqBody)) {
            return ProtocolManager.Packet.dataInCorrect(Command.COMMAND_APPLY_LOG_RESP, ImStatus.C10020, imChannelContext);
        }
        String userId = confirmApplyReqBody.getUserId();
        String applyId = confirmApplyReqBody.getApplyId();
        Integer type = confirmApplyReqBody.getType();
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(applyId) || Objects.isNull(type) || !ImmutableList.of(1, -1).contains(type)) {
            return ProtocolManager.Packet.dataInCorrect(Command.COMMAND_APPLY_LOG_RESP, ImStatus.C10020, imChannelContext);
        }
        ImServerConfig imServerConfig = ImConfig.Global.get();
        MessageHelper messageHelper = imServerConfig.getMessageHelper();
        ApplyFriendDO applyFriendDO = messageHelper.getMyWaitForApplyLog(userId, applyId);
        if (Objects.isNull(applyFriendDO)) {
            return ProtocolManager.Packet.dataInCorrect(Command.COMMAND_APPLY_LOG_RESP, ImStatus.C10020, imChannelContext);
        }
        //update apply log
        messageHelper.updateApplyFriendLog(userId, applyId, type);
        //pass
        if (type == 1) {
            FriendCmdProcessor singleProcessor = getSingleProcessor(FriendCmdProcessor.class);
            User applyUser = singleProcessor.addFriend(userId, applyId);
            User user = ImServerKit.getUser(imChannelContext);
            if (Objects.nonNull(user)) {
                List<User> users = user.getFriends().get(0).getUsers();
                if (users.stream().noneMatch(m -> m.getUserId().equalsIgnoreCase(applyId))) {
                    users.add(applyUser);
                }
                JimServerAPI.bindUser(imChannelContext, userId);
                JimServerAPI.bindUser(imChannelContext, applyId);
                ImPacket chatPacket = new ImPacket(Command.COMMAND_CONFIRM_APPLY_RESP, new RespBody(Command.COMMAND_CONFIRM_APPLY_RESP, applyFriendDO).toByte());
                chatPacket.setSynSeq(packet.getSynSeq());
                JimServerAPI.sendToUser(applyId, chatPacket);
            }
        }

        return ProtocolManager.Converter.respPacket(resPacket, imChannelContext);
    }

}
